﻿$(function () {
    switchMenu();
    $("#btn-branch-search").button({
        icons: {
            primary: "ui-icon-circle-zoomout"
        }
    });
})

function switchMenu() {
    var obj = $("#top-static-menu");
    var status = obj.attr("status")
    if (status == "off") {
        $("#menu-switch").text("收起功能菜单");
        $("#menu-switch-ico").removeClass("ui-icon-circle-triangle-e").addClass("ui-icon-circle-triangle-s");
        obj.attr("status", "on");
        $("#function-table").fadeIn("slow").slideDown("slow");
    } else {
        $("#menu-switch").text("展开功能菜单");
        $("#menu-switch-ico").removeClass("ui-icon-circle-triangle-s").addClass("ui-icon-circle-triangle-e");
        obj.attr("status", "off");
        $("#function-table").fadeOut("slow").slideUp("slow");
    }
}

function go(url, fnType) {
    var category = $("#BranchCategory").val();
    if (category > 0) {
        redirectToFunction(url, category, fnType);
    } else {
        $("#branch_category_search").dialog({
            modal: true,
            width: 600,
            height: 350,
            buttons: {
                "确定": function () {
                    var selecteds = $("#branch_search_result .ui-state-active")
                    if (selecteds.length <= 0) {
                        jalert("请选择营业部！");
                        return;
                    }
                    redirectToFunction(url, $(selecteds[0]).attr("cid"), fnType);
                },
                "取消": function () {
                    $(this).dialog("close");
                }
            }
        });
        return;
    }
}

function searchCategories() {
    var url = GetUrl("SearchCategories", "Home");
    var name = $("#branch_search").val();
    $("#branch_search_result").html("<div><span class='ui-icon ui-icon-mail-open' style='float:left'></span>加载中，请稍后...</div>");

    $.post(url, { name: name }, function (data) {
        if (data == null || data.length <= 0) {
            $("#branch_search_result").html("找不到相关记录！");
        } else {
            var html = "";
            for (var i in data) {
                html += "<a class='ui-state-default ui-corner-all category-search-item' href='javascript:void(0);' cid='" + data[i].Category_ID + "' onclick='selectFoundCategories(this, " + data[i].Category_ID + ")'><span>" + data[i].Category_Name + "</span></a>";
            }
            $("#branch_search_result").html(html);
        }
    });
}

function selectFoundCategories(obj, id) {
    $("#branch_search_result a").removeClass("ui-state-active");
    $(obj).addClass("ui-state-active");
}

function redirectToFunction(url, category, type) {
    //    var redirectUrl = setUrlParam("branchcategory", category, url);
    //    redirectUrl = setUrlParam("function", type, redirectUrl);
    setCookie("branch-category", category);
    window.location.href = url;
}